package com.larissa.android.quiz.fragments;

import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;

import com.larissa.android.quiz.QuestionActivity;
import com.larissa.android.quiz.R;
import com.larissa.android.quiz.databinding.FragmentGeoQuestionEditorBinding;
import com.larissa.android.quiz.question.Question;
import com.larissa.android.quiz.question.QuestionRepository;

public class GeoQuestionEditorFragment extends BasicFragment {

    private FragmentGeoQuestionEditorBinding binding;
    private Question questionToBeShown;
    private Question originalQuestion;
    private QuestionEditorViewModel vm;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_geo_question_editor;
    }

    @Override
    public void initView(View rootView) {
        binding = DataBindingUtil.bind(rootView);
        vm = new ViewModelProvider(this).get(QuestionEditorViewModel.class);
        if (getArguments() != null) {
            if(vm.getOriginalQuestion() == null && vm.getQuestionToBeShown() == null) {
                vm.setOriginalQuestion(QuestionFragmentArgs.fromBundle(getArguments()).getQuestion());
                vm.setQuestionToBeShown(vm.getOriginalQuestion().clone());
            }
        }
        questionToBeShown = vm.getQuestionToBeShown();
        originalQuestion = vm.getOriginalQuestion();
        binding.setQuestion(questionToBeShown);
        binding.btnSave.setOnClickListener(v -> {
            if (!originalQuestion.equals(questionToBeShown)) {
                originalQuestion.setQuestionText(questionToBeShown.getQuestionText());
                originalQuestion.setQuestionAnswer(questionToBeShown.getQuestionAnswer());
                if (!originalQuestion.hasTheSameCategoryWith(questionToBeShown)) {
                    if (originalQuestion.getCategory().equals(Question.GEO)) {
                        QuestionRepository.getInstance().getGeoQuestions().remove(originalQuestion);
                        QuestionRepository.getInstance().getMathQuestions().add(originalQuestion);
                    } else if (originalQuestion.getCategory().equals(Question.MATH)) {
                        QuestionRepository.getInstance().getMathQuestions().remove(originalQuestion);
                        QuestionRepository.getInstance().getGeoQuestions().add(originalQuestion);
                    }
                    originalQuestion.setCategory(questionToBeShown.getCategory());
                }
                QuestionRepository.getInstance().getQuestionsToBeUpdated().add(originalQuestion);
            }
        });
        binding.btnCancel.setOnClickListener(v -> {
            questionToBeShown.setQuestionText(originalQuestion.getQuestionText());
            questionToBeShown.setBoolAnswer(Boolean.parseBoolean(originalQuestion.getQuestionAnswer()));
            questionToBeShown.setCategory(originalQuestion.getCategory());
        });
    }

    @Override
    public void onDestroyView() {
        if (getActivity() instanceof QuestionActivity) {
            QuestionActivity activity = (QuestionActivity) getActivity();
            NavHostFragment navHostFragment = activity.getNavHostFragment();
            navHostFragment.getChildFragmentManager().getFragments().forEach(fragment -> {
                if (fragment instanceof QuestionFragment) {
                    QuestionFragment questionFragment = (QuestionFragment) fragment;
                    questionFragment.notifyRecyclerViewAdapter();
                }
            });
        }
        super.onDestroyView();
    }
}
